﻿using System;
using System.Runtime.Serialization;

namespace Pegasus.Diagnostics.Contracts
{
	/// <summary>
	/// Thrown when a code contract condition fails.
	/// </summary>
	[Serializable]
	internal sealed class ContractException : Exception
	{
		// Local Instance Values
		private ContractFailureKind m_failureKind;

		/// <summary>
		/// Initializes a new instance of the <see cref="ContractException"/> class.
		/// </summary>
		public ContractException()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ContractException"/> class.
		/// </summary>
		/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
		/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
		/// <exception cref="T:System.ArgumentNullException">
		/// The <paramref name="info"/> parameter is null.
		/// </exception>
		/// <exception cref="T:System.Runtime.Serialization.SerializationException">
		/// The class name is null or <see cref="P:System.Exception.HResult"/> is zero (0).
		/// </exception>
		private ContractException( SerializationInfo info, StreamingContext context )
			: base( info, context )
		{
			m_failureKind = (ContractFailureKind) info.GetInt32( "FailureKind" );
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ContractException"/> class.
		/// </summary>
		/// <param name="failureKind">Kind of the failure.</param>
		/// <param name="userMessage">The user message.</param>
		public ContractException( ContractFailureKind failureKind, string userMessage )
			: base( userMessage )
		{
			m_failureKind = failureKind;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ContractException"/> class.
		/// </summary>
		/// <param name="failureKind">Kind of the failure.</param>
		/// <param name="userMessage">The user message.</param>
		/// <param name="innerException">The inner exception.</param>
		public ContractException( ContractFailureKind failureKind, string userMessage, Exception innerException )
			: base( userMessage, innerException )
		{
			m_failureKind = failureKind;
		}

		/// <summary>
		/// When overridden in a derived class, sets the <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with information about the exception.
		/// </summary>
		/// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
		/// <param name="context">The <see cref="T:System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
		/// <exception cref="T:System.ArgumentNullException">
		/// The <paramref name="info"/> parameter is a null reference (Nothing in Visual Basic).
		/// </exception>
		/// <PermissionSet>
		/// 	<IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Read="*AllFiles*" PathDiscovery="*AllFiles*"/>
		/// 	<IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="SerializationFormatter"/>
		/// </PermissionSet>
		public override void GetObjectData( SerializationInfo info, StreamingContext context )
		{
			base.GetObjectData( info, context );
			info.AddValue( "FailureKind", m_failureKind );
		}

		/// <summary>
		/// Gets the kind of the failure.
		/// </summary>
		/// <value>The kind of the failure.</value>
		public ContractFailureKind FailureKind
		{
			get
			{
				return m_failureKind;
			}
		}
	}
}
